<?php

require_once "include.php";

if($method == "GET"){

	if($_REQUEST["user1_id"]){
		$user1_id = $_REQUEST['user1_id'];

		$query = "SELECT * FROM friend WHERE user1_id=$user1_id";

	} 

	$result = mysql_query($query) or die(mysql_error());  

	$return_arr = array();

	while($row1 = mysql_fetch_array( $result )){
		$user2_id = $row1["user2_id"];
		
		$query = "SELECT * FROM user WHERE id=$user2_id";
		$result2 = mysql_query($query) or die(mysql_error());

		while($row = mysql_fetch_array( $result2 )){

			$user = array(
				"id" => $row["id"],
				"username" => $row["username"],
				"fullname" => $row["fullname"],
				"image" => $row["image"]
				);
			$return_arr[] = $user;
		}
	}

	echo json_encode($return_arr);
}
else if($method == "POST"){
	$user1_id = mysql_real_escape_string( $_REQUEST['user1_id'] );
	$user2_id = mysql_real_escape_string( $_REQUEST['user2_id'] );
	
	if($user1_id && $user2_id){
		
			$query = "INSERT INTO friend(user1_id, user2_id) VALUES($user1_id, $user2_id)";
			$result = mysql_query($query);
			$query = "INSERT INTO friend(user2_id, user1_id) VALUES($user1_id, $user2_id)";
			$result = mysql_query($query);
	
		$result = mysql_query($query);
		if($result){
			echo json_encode("true");
		}else{
			$error = array("error" =>  mysql_error());
			echo json_encode($error);
		}
	}
	
}

?>